package models

import (
	"github.com/astaxie/beego/orm"
	"gosync/utils"
	"errors"
	"strconv"
)

type Project struct {
	Id              int `json:"id"`
	Name           	string `json:"name"`
	Account         string `json:"account"`
	From_path       string `json:"from_path"`
	To_tag			string `json:"to_tag"`
	Exclude			string `json:"exclude"`
	BaseTable
}

type BindIp struct {
	Project_id int `json:"project_id"`
	Machine_id int `json:"machine_id"`
}

type ProjectQueryParam struct {
	BaseQueryParam
}

func (pj *Project) TableName() string {
	return "qi_project"
}
func (pj *Project) BindTableName() string {
	return "qi_bind_mc"
}

func (pj *Project) BindIp(project_id int, ips []string) error{

	o := orm.NewOrm()

	//先删除相关所有关联
	o.Raw(" DELETE FROM "+pj.BindTableName()+" WHERE `project_id` = ?", project_id).Exec()
	for _, item:=range ips{
		_,err := o.Raw("INSERT INTO "+pj.BindTableName()+" (`project_id`, `machine_id`) VALUES (?, ?)", project_id, item).Exec()
		if err != nil {
			return errors.New("操作失败")
		}
	}
	return  nil
}

//查询指定项目ip权限
func (pj *Project) GetBindIp(project_id int) ([]orm.Params, error){
	o := orm.NewOrm()
	var lists []orm.Params
	_, err := o.Raw("SELECT project_id,machine_id FROM "+pj.BindTableName()+" WHERE project_id = ?", project_id).Values(&lists)
	if err != nil {
		return nil, err
	}
	mc_ids := []int{}
	sql_txt :=""
	for _, item:=range lists{
		int_id,_ := strconv.Atoi(item["machine_id"].(string))
		mc_ids = append(mc_ids, int_id)
		sql_txt += ",?"
	}
	sql_txt = utils.Substr(sql_txt, 1, len(sql_txt)-1)

	var mclist []orm.Params
	_, err = o.Raw("select ip from qi_machine where id in("+sql_txt+")", mc_ids).Values(&mclist)
	return mclist, nil

}

//获取分页数据
func (pj *Project) PageList(pagenum int) ([]*Project, int64) {
	query := orm.NewOrm().QueryTable(pj.TableName())
	data := make([]*Project, 0)
	limit := (pagenum-1)*10
	total, _ := query.Count()
	query.OrderBy("-Id").Limit(10, limit).All(&data)
	return data, total
}

//添加项目
func (pj *Project) AddPj() error {
	o := orm.NewOrm()
	pj.BaseTable.Created_at = utils.GetNowTime()
	pj.BaseTable.Updated_at = utils.GetNowTime()

	info, _ := pj.GetOneByAccount(pj.Account)

	if info != nil {
		return errors.New("项目已经存在了！")
	}

	// 插入数据
	_, err := o.Insert(pj)
	if err != nil {
		return err
	}
	return nil
}

func (pj *Project) EditPj() (int64, error) {
	o := orm.NewOrm()
	pj.BaseTable.Updated_at = utils.GetNowTime()

	if num, err := o.Update(pj, "Id", "Name", "Account","From_path", "Exclude", "Updated_at"); err == nil {
		return num, err
	}else{
		return 0, err
	}
}

//通过ID获取记录
func (pj *Project) GetOne(id int) (*Project, error){
	o := orm.NewOrm()
	pj.Id = id
	err := o.Read(pj)
	if err != nil {
		return nil, err
	}
	return pj, nil
}

func (pj *Project) GetBindMc(id int) ([]orm.Params, error){
	o := orm.NewOrm()
	var lists []orm.Params
	_, err := o.Raw("SELECT project_id,machine_id FROM "+pj.BindTableName()+" WHERE project_id = ?", id).Values(&lists)

	if err != nil {
		return nil, err
	}

	return lists, nil
}


//通过ID获取记录
func (pj *Project) GetOneByAccount(account string) (*Project, error){
	o := orm.NewOrm()
	where := Project{}
	qs := o.QueryTable(where.TableName())
	err := qs.Filter("account", account).One(&where)

	if err != nil {
		return nil, err
	}
	return &where, nil
}

//删除项目
func (pj *Project) DelPj(id int)(int64, error){
	o := orm.NewOrm()
	where := Project{}
	where.Id = id
	//删除关联数据
	o.Raw(" DELETE FROM "+pj.BindTableName()+" WHERE `project_id` = ?", id).Exec()
	if num, err := o.Delete(&where); err == nil {
		return num, err
	}else{
		return 0, err
	}
}
